package com.uneed.api.util;

import java.io.IOException;
import java.io.InputStream;

public class WatchedInputStream extends InputStream
{
  public static final int DEFAULT_NOTIFY_BYTES = 4095;
  private Listener listener;
  private int notifyBytes;
  private int remains;
  private InputStream stream;

  public WatchedInputStream(InputStream paramInputStream)
  {
    this(paramInputStream, 4095);
  }

  public WatchedInputStream(InputStream paramInputStream, int paramInt)
  {
    this.stream = paramInputStream;
    this.notifyBytes = paramInt;
  }

  public int available()
    throws IOException
  {
    return this.stream.available();
  }

  public void close()
    throws IOException
  {
    this.stream.close();
  }

  /** @deprecated */
  public void mark(int paramInt)
  {
//    monitorenter;
    try
    {
      throw new UnsupportedOperationException();
    }
    finally
    {
//      monitorexit;
    }
  }

  public boolean markSupported()
  {
    return false;
  }

  public int read()
    throws IOException
  {
    int i = this.stream.read();
    if (i >= 0)
    {
      int k = 1 + this.remains;
      this.remains = k;
      if (k <= this.notifyBytes)
      {
    	    int j = this.remains;
    	    this.remains = 0;
    	    if (this.listener != null)
    	      this.listener.notify(j);
      }
    }
    return i;
  }

  public int read(byte[] paramArrayOfByte)
    throws IOException
  {
    return read(paramArrayOfByte, 0, paramArrayOfByte.length);
  }

  public int read(byte[] paramArrayOfByte, int paramInt1, int paramInt2)
    throws IOException
  {
    int i = this.stream.read(paramArrayOfByte, paramInt1, paramInt2);
    if (i < 0)
    {
      int k = this.remains;
      this.remains = 0;
      if (this.listener != null)
        this.listener.notify(k);
    } else {
      this.remains = (i + this.remains);
      if (this.remains > this.notifyBytes)
      {
    	  int j = this.remains;
          this.remains %= this.notifyBytes;
          if (this.listener != null)
        	  this.listener.notify(j - this.remains);
      }
    }
    return i;
  }

  /** @deprecated */
  public void reset()
    throws IOException
  {
//    monitorenter;
    try
    {
      throw new IOException("not supported operation: reset");
    }
    finally
    {
//      monitorexit;
    }
  }

  public void setListener(Listener paramListener)
  {
    this.listener = paramListener;
  }

  public long skip(long paramLong)
    throws IOException
  {
    throw new IOException("not supported operation: skip");
  }

  public static abstract interface Listener
  {
    public abstract void notify(int paramInt);
  }
}
